package myapp.camera;

import myapp.EnumAndConstans.Constants;
import myapp.googlemap.R;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class Settings extends Activity {

  ToggleButton toggleButton_autoChangeScreen;
  ToggleButton toggleButton_autoFilterServicesByOrientation;
  ToggleButton toggleButton_displayServiceIcon;
  ToggleButton toggleButton_autoUpdateLocation;
  
  boolean bool_autoChangeScreen = false;
  boolean bool_autoFilterServicesByOrientation = false;
  boolean bool_displayServiceIcon = false;
  boolean bool_autoUpdateLocation = true;

  
  SharedPreferences prefs;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_settings);
    

    toggleButton_autoChangeScreen = (ToggleButton)findViewById(R.id.toggleButton_autoChangeScreen);
    toggleButton_autoFilterServicesByOrientation = (ToggleButton)findViewById(R.id.toggleButton_autoFilterServicesByOrientation);
    toggleButton_displayServiceIcon = (ToggleButton)findViewById(R.id.toggleButton_displayServiceIcon);
    toggleButton_autoUpdateLocation = (ToggleButton)findViewById(R.id.toggleButton_autoUpdateLocation);
    
    Context context = getApplicationContext();
    prefs = PreferenceManager.getDefaultSharedPreferences(context);
    updateUIFromPreferences();
    
    if (bool_autoFilterServicesByOrientation == false)
    	toggleButton_displayServiceIcon.setEnabled(false);
    
    toggleButton_autoChangeScreen.setOnCheckedChangeListener(new OnCheckedChangeListener() {	
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			if (isChecked) {
	            // The toggle is enabled
				bool_autoChangeScreen = true;
	        } else {
	            // The toggle is disabled
	        	bool_autoChangeScreen = false;
	        }
			
		}
	});
    
    toggleButton_autoFilterServicesByOrientation.setOnCheckedChangeListener(new OnCheckedChangeListener() {	
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			if (isChecked) {
	            // The toggle is enabled
				bool_autoFilterServicesByOrientation = true;
				toggleButton_displayServiceIcon.setEnabled(true);
	        } else {
	            // The toggle is disabled
	        	bool_autoFilterServicesByOrientation = false;
	        	toggleButton_displayServiceIcon.setEnabled(false);
	        }
			
		}
	});
    
    toggleButton_displayServiceIcon.setOnCheckedChangeListener(new OnCheckedChangeListener() {	
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			if (isChecked) {
	            // The toggle is enabled
				bool_displayServiceIcon = true;
	        } else {
	            // The toggle is disabled
	        	bool_displayServiceIcon = false;
	        }
			
		}
	});
    
    toggleButton_autoUpdateLocation.setOnCheckedChangeListener(new OnCheckedChangeListener() {	
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			if (isChecked) {
	            // The toggle is enabled
				bool_autoUpdateLocation = true;
	        } else {
	            // The toggle is disabled
	        	bool_autoUpdateLocation = false;
	        }
			
		}
	});
   

    Button okButton = (Button) findViewById(R.id.button_OK);
    okButton.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {
        savePreferences();
        Settings.this.setResult(RESULT_OK);
        finish();
      }
    });

    Button cancelButton = (Button) findViewById(R.id.button_Cancel);
    cancelButton.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {
        Settings.this.setResult(RESULT_CANCELED);
        finish();
      }
    });
  }  
 
  
  private void updateUIFromPreferences() {
    bool_autoChangeScreen = prefs.getBoolean(Constants.autoChangeScreen, false);
    bool_autoFilterServicesByOrientation = prefs.getBoolean(Constants.autoFilterServicesByOrientation, false);
    bool_displayServiceIcon = prefs.getBoolean(Constants.displayServiceIcon, false);
    bool_autoUpdateLocation = prefs.getBoolean(Constants.autoUpdateLocation, true);
    
    toggleButton_autoChangeScreen.setChecked(bool_autoChangeScreen);
    toggleButton_autoFilterServicesByOrientation.setChecked(bool_autoFilterServicesByOrientation);
    toggleButton_displayServiceIcon.setChecked(bool_displayServiceIcon);
    toggleButton_autoUpdateLocation.setChecked(bool_autoUpdateLocation);
  }
  
  private void savePreferences() {
    Editor editor = prefs.edit();
    editor.putBoolean(Constants.autoChangeScreen, bool_autoChangeScreen);
    editor.putBoolean(Constants.autoFilterServicesByOrientation, bool_autoFilterServicesByOrientation);
    editor.putBoolean(Constants.displayServiceIcon, bool_displayServiceIcon);
    editor.putBoolean(Constants.autoUpdateLocation, bool_autoUpdateLocation);
    editor.commit();  
  }
}